package com.it.schoolbookshop_back.interceptors;


import com.it.schoolbookshop_back.enums.UserRoleEnums;
import com.it.schoolbookshop_back.utils.UserContext;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;



@Component
public class AdminInterfaceVerifyRole implements HandlerInterceptor {


    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String path = request.getRequestURI();

        if (path.contains("/api/admin")) {
                return UserContext.getRole() == UserRoleEnums.ADMIN.name();
        }
        return true;
    }

    @Override //请求（接口）结束后执行
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        //清空ThreadLocal中的数据
        UserContext.clear();
    }
}
